import { MenuApi } from '@/api/interface/menu'

/**
 * 转换菜单
 * @param menus
 */
export const generatorMenuList = (menus: MenuApi.Menu[]) => {
  if (menus[0]?.title !== '顶层菜单') return []
  menus = menus[0].children || []

  const mapMenuList = (menus: MenuApi.Menu[]) => {
    let menuList: Menu.MenuOptions[] = []

    menus.forEach((menu) => {
      let tempMenu: Menu.MenuOptions = {
        path: menu.path,
        name: menu.name,
        component: menu.component,
        redirect: menu.redirect,
        children: [],
        meta: {
          icon: menu.icon,
          title: menu.title,
          isLink: menu.is_link,
          isHide: !!menu.is_hide,
          isFull: !!menu.is_full,
          isAffix: !!menu.is_affix,
          isKeepAlive: !!menu.is_keep_alive
        }
      }
      menuList.push(tempMenu)

      if (menu.children && menu.children.length) {
        tempMenu.children = mapMenuList(menu.children)
      }
    })

    return menuList
  }

  return mapMenuList(menus)
}
